package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class VodServiceImpl implements VodService {



    @Autowired
    VodConstantProperties vodConstantProperties;

    @Override
    public Map<String, String> uploadTrack(MultipartFile file) {
        Map<String, String> map = new HashMap<>();
        String filePath = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);
        VodUploadClient client = new VodUploadClient(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
        VodUploadRequest request = new VodUploadRequest();
        // 2.构造上传请求对象
        request.setMediaFilePath(filePath);
        try {
            VodUploadResponse response = client.upload(vodConstantProperties.getRegion(), request);
            log.info("Upload FileId = {}", response.getFileId());

            map.put("mediaFileId", response.getFileId());//文件唯一标识符号
            map.put("mediaUrl", response.getMediaUrl());//文件路径
        } catch (Exception e) {
            // 业务方进行异常处理
            throw new GuiguException(201, "上传声音到vod中失败");
        }
        return map;
    }

    @Override
    public TrackMediaInfoVo getMedTrackInfo(String mediaFileId) {
        try{
            //构建参数
            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
            ClientProfile clientProfile = new ClientProfile();
            //获取一个客户端
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion(), clientProfile);
            //构建一个请求
            DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
            //传入媒体Id
            String[] fileIds1 = {mediaFileId};
            req.setFileIds(fileIds1);
            //得到响应
            DescribeMediaInfosResponse describeMediaInfosResponse = client.DescribeMediaInfos(req);
            MediaInfo mediaInfo = describeMediaInfosResponse.getMediaInfoSet()[0];
            //获取媒体的基本信息
            MediaBasicInfo basicInfo = mediaInfo.getBasicInfo();
            //获取媒体的url
            String mediaUrl = basicInfo.getMediaUrl();
            //获取媒体的类型
            String type = basicInfo.getType();
            //获取媒体的原数据
            MediaMetaData metaData = mediaInfo.getMetaData();
            //获取媒体的时常
            Float duration = metaData.getDuration();
            //获取媒体的大小
            Long size = metaData.getSize();

            TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();
            trackMediaInfoVo.setSize(size);
            trackMediaInfoVo.setDuration(duration);
            trackMediaInfoVo.setMediaUrl(mediaUrl);
            trackMediaInfoVo.setType(type);

            return trackMediaInfoVo;
        } catch (TencentCloudSDKException e) {
          throw new GuiguException(201,"获取媒体的信息");
        }
    }


}
